#coding=utf-8
import unittest
from selenium import webdriver
from appium import webdriver
from Public import BasePage
from time import sleep
from nav_plugin_op import nav_plugin_factory
from selenium.common.exceptions import WebDriverException
import os
from Tool.usefulTools import userfulToolsFactory
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import settings

navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_oprate_instance = element_Oprate()
basicAction = BasicAction()
Selfdeal_Paramter_instances = BaseConstant.Selfdeal_Paramter
# nav_name = "工作"
# plugin_name = "自行处置"
nav_name = ["导航栏工作-for-自行处置",'xpath',"//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.FrameLayout/android.widget.RadioButton"]
plugin_name = ["插件列表-自行处置",'name','自行处置']
report_desc_keys = "自行处置问题上报"

class self_deal_factory():

	def open_selfdeal_plug(self,driver,file_path):

		try:
			# # 点击工作的导航，打开工作列表
			element_oprate_instance.element_click(nav_name,file_path)
			driver.swipe(400,1000,400,300,1200)
			# 点击"自行处理"的插件，打开插件
			sleep(5)
			element_oprate_instance.element_click(plugin_name,file_path)
			element_oprate_instance.element_save_pic(settings.pic_file_path, 'selfdeal_plug_open')
			sleep(10)
			if element_oprate_instance.element_display_commit(Selfdeal_Paramter_instances.my_case_name, file_path):
				element_oprate_instance.element_save_pic(file_path, 'open_selfdeal_plug_successful')
			else:
				element_oprate_instance.element_save_pic(file_path, 'open_selfdeal_plug_failed')
				print '自行处置打开失败'
				raise
		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'open_selfdeal_plug_fail')
			raise

	def add_selfdeal_case(self,driver,file_path):
		#添加自行处置案件
		try:
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.add_case_id, file_path)
			#选择类型
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_eventtype_id, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_eventtype_keys, file_path)
			#选择大类
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_maintype_id, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.Main_sub_type_list_first, file_path)
			#选择小类
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_subtype_id, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.Main_sub_type_list_first, file_path)
			#选定位置
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.location_desc_id, file_path)
			sleep(5)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.map_bottom_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.comfirm_id, file_path)
			#添加描述
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_desc_id, file_path)
			element_oprate_instance.element_send_keys(Selfdeal_Paramter_instances.report_desc_id,report_desc_keys.decode("utf-8"), file_path)
			#添加处理前照片
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.before_deal_xpath, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.add_photo_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.select_photo_id, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.comfirm_photo_id, file_path)
			#添加处理后照片
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.after_deal_xpath, file_path)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.tool_camera_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.TakePhoto_id, file_path)
			sleep(3)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.btnSave_id, file_path)
			sleep(2)			
			#截图
			element_oprate_instance.element_save_pic(file_path, 'selfdeal_case')
			driver.swipe(400,250,400,50,800)
			sleep(2)	
			#点击自处理按钮
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.report_btn_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.comfirm_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.comfirm_id, file_path)
			sleep(2)
			element_oprate_instance.element_save_pic(file_path, 'selfdeal_case_successful')
			element_oprate_instance.element_click(Selfdeal_Paramter_instances.exit_name, file_path)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'selfdeal_case_fail')
			raise

if __name__ == '__main__':
	unittest.main()
